package com.example.newhbsj;

import java.util.ArrayList;
import java.util.Iterator;

import com.example.bean.User;
import com.example.dao.Dbhelp;
import com.example.newhbsj.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources.Theme;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;






public class LogonActivity extends Activity {

	
	private EditText ed;
	private CheckBox cb;
	private Button tel;
	private EditText names;
	private ImageButton exit;
	public static  LogonActivity mainactivity;
	private Button login1;
	private Dbhelp dbhelp;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		setContentView(R.layout.activity_logon);
		mainactivity=this;
		
		//������ݿ�
		dbhelp=new Dbhelp(this, "users.db", null, 1);
		
		
		
		
		//�����Ļ����ط�������Զ�����
		findViewById(R.id.totals).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switch (v.getId()) {  
			    case R.id.totals:  
			         InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
			         imm.hideSoftInputFromWindow(v.getWindowToken(), 0);  
			        break;  
			}
		}
		});
		
		
		
		
		//������ʾ������
		names=(EditText) findViewById(R.id.names);
		ed=(EditText) findViewById(R.id.password);
		cb=(CheckBox)findViewById(R.id.checkbox);
		exit=(ImageButton) findViewById(R.id.exit);
		login1=(Button) findViewById(R.id.login1);
		
		login1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SQLiteDatabase sql=dbhelp.getReadableDatabase();
				
				
				boolean b=false;
				String account=names.getText().toString();
				String passwd=ed.getText().toString();
				if(account.length()!=0&&passwd.length()!=0){
				Cursor curson=sql.rawQuery("select * from users  where account='"+account+"' and passwd='"+passwd+"';",null);		
				if(curson.getCount()!=0){
					b=true;
				}else{
					b=false;
				}
				
				curson.close();
		
				if(b){
					Toast.makeText(LogonActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
					Intent intent=new Intent(LogonActivity.this,MainActivity.class);
					startActivity(intent);
					finish();
				}else{
					Toast.makeText(LogonActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
				}
				
				}else{
					Toast.makeText(LogonActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
					
				}
				}
				
				
//				// TODO Auto-generated method stub
//			SharedPreferences sp=getSharedPreferences("data", MODE_PRIVATE);
//			String account=sp.getString("account", "");	
//			String passwd=sp.getString("passwd", "");	
//	
////			System.out.println(account+" asd"+passwd);
////			
//			if(names.getText().toString().equals(null)||ed.getText().toString().equals(null)){
//				Toast.makeText(MainActivity.this, "�û�������벻��Ϊ��", Toast.LENGTH_SHORT).show();	
//			}else{
//				if(names.getText().toString().equals(account)){
//					if(ed.getText().toString().equals(passwd)){
						
//					}else{
//						Toast.makeText(MainActivity.this, "�������", Toast.LENGTH_SHORT).show();
//					}
//				}else{
//					Toast.makeText(MainActivity.this, "�û������", Toast.LENGTH_SHORT).show();
//				}
//				
//				
//			}
//			
			
			
			
		});
		
		
		
		exit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked){
					ed.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
					
				}else{
					ed.setTransformationMethod(PasswordTransformationMethod.getInstance());
				}
				
				
				
			}
		});
		
		tel=(Button) findViewById(R.id.tel);
		tel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				dbhelp.getWritableDatabase();
				Intent i=new Intent(LogonActivity.this,RegisterActivity.class);
				startActivity(i);
			}
			
		});
		
		
	

	}
	
		
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_BACK){
			AlertDialog.Builder alertDialog=new AlertDialog.Builder(LogonActivity.this);
			alertDialog.setTitle("����");
			alertDialog.setMessage("������������");
			alertDialog.setPositiveButton("������", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			
			
			alertDialog.setNegativeButton("�Ժ�����", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					finish();
				}
			});
			alertDialog.show();
		}
		return false;
	}


	
	//��֤�˻�����
	
	
	

	

}

